//
//  CHSViewController.m
//  ZXTimer
//
//  Created by simon on 05/15/2023.
//  Copyright (c) 2023 simon. All rights reserved.
//

#import "CHSViewController.h"
#import "MyObject2.h"
#import <objc/message.h>

@interface CHSViewController ()

@property (nonatomic, strong) NSThread *thread;

@end

@implementation CHSViewController


- (void)viewDidLoad
{
    [super viewDidLoad];
    MyObject2 *obj = [MyObject2 new];
    struct objc_super superObj = {obj, obj.superclass};
    ((void(*)(struct objc_super*,SEL,NSString *))objc_msgSendSuper)(&superObj,@selector(hello:),@"LiLi");
//    obj.lineSpace = 5;
//    NSLog(@"%@",@(obj.lineSpace));
//
	// Do any additional setup after loading the view, typically from a nib.
//    [self observeRunLoop];

    self.thread = [[NSThread alloc] initWithBlock:^{
        NSLog(@"子线程%@执行",[NSThread currentThread]);
        NSLog(@"子线程runloop%@",[NSRunLoop currentRunLoop]);
        [self observeRunLoop];

      }];
    self.thread.name = @"子线程A";
    [self.thread start];
        
}

//@property (nonatomic, strong) NSThread *thread;
- (void)test{
    //获取属性
    const char *name = [@"thread" UTF8String];
    objc_property_t property = class_getProperty([self class], name);
    
    //获取属性名
    const char *propertyName = property_getName(property);
    NSString *propertyNameStr = [NSString stringWithUTF8String:propertyName];
    
    //获取属性特性字符串
    const char *attributeName = property_getAttributes(property);
    //T@\"NSThread\",&,N,V_thread
    NSString *attributesStr = [NSString stringWithUTF8String:attributeName];
    //
    char *attributeValue = property_copyAttributeValue(property, "N");

    unsigned int outCount, i;
    objc_property_attribute_t *attributeList = property_copyAttributeList(property, &outCount);
    for (i = 0; i<outCount; i++)
    {
        objc_property_attribute_t *a = &attributeList[i];
        
    }
    
    
}

- (BOOL)containsString:(NSString *)string{
    return YES;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (void)mainRunLoopPerformAction{
    
}

- (void)currentRunLoopAction{
    
}



//- (void)perform{
//    [self performSelector:@selector(log) withObject:nil afterDelay:2];
//    [self performSelectorOnMainThread:@selector(log) withObject:nil waitUntilDone:NO];
//    NSThread *thread = [NSThread currentThread];
//    [self performSelector:@selector(log) onThread:self.thread withObject:nil waitUntilDone:NO];
    //线程间通信，在指定线程中执行log方法
//    [self performSelector:@selector(log) onThread:self.thread withObject:nil waitUntilDone:YES];
//}


- (void)observeRunLoop{
    
    CFRunLoopObserverRef observer = CFRunLoopObserverCreateWithHandler(CFAllocatorGetDefault(), kCFRunLoopAllActivities, YES, 0, ^(CFRunLoopObserverRef observer, CFRunLoopActivity activity) {
        switch (activity) {
            case kCFRunLoopEntry:
                NSLog(@"即将进入runloop");
                break;
            case kCFRunLoopBeforeTimers:
                NSLog(@"runloop即将处理Timer");
                break;
            case kCFRunLoopBeforeSources:
                NSLog(@"runloop即将处理Sources");
                break;
            case kCFRunLoopBeforeWaiting:
                NSLog(@"runloop（线程）即将进行休眠");
                break;
            case kCFRunLoopAfterWaiting:
                NSLog(@"runloop（线程）刚从休眠中唤醒");
                break;
            case kCFRunLoopExit:
                NSLog(@"即将退出runloop");
                break;
            default:
                break;
        }
    });
    //将CFRunLoopObserver对象添加到当前runloop的运行循环模式。
    CFRunLoopAddObserver(CFRunLoopGetCurrent(), observer, kCFRunLoopDefaultMode);
    //获取当前线程（子线程）的RunLoop对象
    NSRunLoop *runloop = [NSRunLoop currentRunLoop];
//    //添加一个端口输入源，防止runloop因为没有任何源而退出
    [self performSelector:@selector(currentRunLoopAction) withObject:nil afterDelay:10.f];
    [runloop run];
    CFRelease(observer);
}


/*
- (void)threadMain{
    NSRunLoop *myRunLoop = [NSRunLoop currentRunLoop];
    // Create a run loop observer and attach it to the run loop.
    // 创建一个运行循环观察者并将其附加到运行循环中。
    CFRunLoopObserverContext context = {0, (__bridge void *)(self), NULL, NULL, NULL};
    CFRunLoopObserverRef observer = CFRunLoopObserverCreate(kCFAllocatorDefault, kCFRunLoopAllActivities, YES, 0, &myCFRunLoopObserverCallBack, &context);
    if (observer) {
        //将CFRunLoopObserver对象添加到当前runloop的运行循环模式。
//        CFRunLoopAddObserver(CFRunLoopGetCurrent(), observer, kCFRunLoopDefaultMode);
        CFRunLoopRef cfLoop = [myRunLoop getCFRunLoop];
        CFRunLoopAddObserver(cfLoop, observer, kCFRunLoopDefaultMode);
    }
    // Create and schedule the timer.
    [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(doFireTimer:) userInfo:nil repeats:YES];
    NSInteger loopCount = 10;
    do {
        // Run the run loop 10 times to let the timer fire.
        [myRunLoop runUntilDate:[NSDate dateWithTimeIntervalSinceNow:1]];
        loopCount--;
    } while (loopCount);
}
*/

//- (void)doFireTimer:(NSTimer *)timer{
//    NSLog(@"123456");
//}

void myCFRunLoopObserverCallBack(CFRunLoopObserverRef observer, CFRunLoopActivity activity, void *info){
    switch (activity) {
        case kCFRunLoopEntry:
            NSLog(@"即将进入runloop");
            break;
        case kCFRunLoopBeforeTimers:
            NSLog(@"runloop即将处理Timer");
            break;
        case kCFRunLoopBeforeSources:
            NSLog(@"runloop即将处理Sources");
            break;
        case kCFRunLoopBeforeWaiting:
            NSLog(@"runloop（线程）即将进行休眠");
            break;
        case kCFRunLoopAfterWaiting:
            NSLog(@"runloop（线程）刚从休眠中唤醒");
            break;
        case kCFRunLoopExit:
            NSLog(@"即将退出runloop");
            break;
        default:
            break;
    }
}


- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
}

//- (void)log{
//    NSLog(@"runloop执行selector");
//}



@end
